package cn.b.sky.mes.cm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.CalendarPattern;
import cn.b.sky.mes.cm.model.CalendarPatternItem;
import cn.b.sky.mes.cm.model.WorkDayType;
import cn.b.sky.mes.cm.model.condition.CalendarPatternCondition;
import cn.b.sky.mes.cm.service.CalendarPatternService;
import cn.b.sky.mes.cm.service.WorkDayTypeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/calendarPattern/*")
public class CalendarPatternController extends SecurityController {
  @Resource
  CalendarPatternService calendarPatternService;

  @Resource
  WorkDayTypeService workDayTypeService;

  @RequestMapping("calendarPatternList")
  public String calendarPatternList(Model model,
      @ModelAttribute("condition") CalendarPatternCondition calendarPatternCondition, String navTabId) {
    calendarPatternCondition.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isEmpty(calendarPatternCondition.getOrderField())) {
      calendarPatternCondition.setOrderField("createDateTime");
    }
    List<CalendarPattern> calendarPatternList = calendarPatternService
        .selectCalendarPatternByCondition(calendarPatternCondition);
    int count = calendarPatternService.selectCalendarPatternCount(calendarPatternCondition);
    calendarPatternCondition.setData(calendarPatternList);
    calendarPatternCondition.setTotalCount(count);
    model.addAttribute("page", calendarPatternCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/calendarPattern/calendarPattern_list";
  }

  @RequestMapping("validateCalendarPattern")
  @ResponseBody
  public boolean validateCalendarPattern(String calendarPatternCode, Model model) {
    if (!StringUtils.isBlank(calendarPatternCode)) {
      return null == calendarPatternService.selectCalendarPatternByCode(getSiteCode(), calendarPatternCode);
    }
    return true;

  }

  @RequestMapping("addCalendarPattern")
  public String addCalendarPattern(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    List<WorkDayType> types = workDayTypeService.selectUnLockWorkDayTypeList(getSiteCode());
    model.addAttribute("types", types);
    return "cm/calendarPattern/calendarPattern_add";
  }

  @RequestMapping("editCalendarPattern")
  public String editCalendarPattern(Model model, String navTabId, String id) {
    CalendarPattern calendarPattern = calendarPatternService.selectCalendarPatternByCode(getSiteCode(), id);
    List<CalendarPatternItem> items = calendarPatternService.selectCalendarPatternItems(getSiteCode(), id);
    List<WorkDayType> types = workDayTypeService.selectUnLockWorkDayTypeList(getSiteCode());
    calendarPattern.setItems(items);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("calendarPattern", calendarPattern);

    model.addAttribute("types", types);
    return "cm/calendarPattern/calendarPattern_add";
  }

  @RequestMapping("saveCalendarPattern")
  @ResponseBody
  public DWZResult saveCalendarPattern(Model model, String id, CalendarPattern calendarPattern,
      HttpServletRequest request) {
    if (calendarPattern == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    calendarPattern.setSiteCode(getSiteCode());

    setCreateInfo(calendarPattern);
    if (StringUtils.isEmpty(id)) {
      calendarPatternService.insertCalendarPattern(calendarPattern);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
    } else {
      calendarPatternService.updateCalendarPattern(calendarPattern);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);

    }

  }

  @RequestMapping("viewCalendarPattern")
  public String viewCalendarPattern(Model model, String navTabId, String id) {
    CalendarPattern calendarPattern = calendarPatternService.selectCalendarPatternByCode(getSiteCode(), id);
    List<CalendarPatternItem> items = calendarPatternService.selectCalendarPatternItems(getSiteCode(), id);
    calendarPattern.setItems(items);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("calendarPattern", calendarPattern);
    return "cm/calendarPattern/calendarPattern_view";
  }

  @RequestMapping("deleteCalendarPattern")
  @ResponseBody
  public DWZResult deleteCalendarPattern(Model model, String navTabId, String id, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = calendarPatternService.deletetCalendarPattern(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }
}
